### This file runs simulations for 100 different initial memberships stored in file "initmems.Rdata"
### We run 150 rounds and 300 sims
### Last Revised and commented: 9 June 2016, JBS

##########################################################################################

# clear workspace
rm(list=ls())

##########################################################################################

# set working directory (needs to be changed by user); path will depend on where the folder is stored within in the file structure
setwd("~/Dropbox/IO imperfect monitoring/GLS_II_REP/")

##########################################################################################

# load libraries
library(reldist)
library(msm)

# load key functions for running the agent-based model
source("functions.R")

# load data for 100 initial memberships
load("initmems.Rdata")

# set random number seed for exact replication
set.seed(1234)

# generate a space with 100 units
x <- rep(0,100)

# set the number of simulations to 300
sims <- 300

# set working directory to subfolder, where the results for the simulations will be stored for the following case: admit pivot = 0.5; decision pivot = 0.5
setwd("~/Dropbox/IO imperfect monitoring/GLS_II_REP/a50d50")

# for each simulation, run the enlarge function (see line 50) and store in a csv file
for (a in 1:sims){
	
	fname <- paste("res_",a,".csv",sep="")
	
	fp <- file(fname,"w")
	
	cat("sim",a,"\n",sep=" ")
	
	for (b in 1:length(samp)) {
	
	results <-enlarge(space = x, members=as.vector(samp[[b]]), sdnmem = 6, a = 0.5, d = 0.5, sdp = 3, R = 150)	
	cat(results,"\n",file=fp)
#	cat("sample",b,"\n",sep=" ")
	rm(results)
	}
	
close(fp)	
	
}

# set working directory to subfolder, where the results for the simulations will be stored for the following case: admit pivot = 0.75; decision pivot = 0.5
setwd("~/Dropbox/IO imperfect monitoring/GLS_II_REP/a75d50/")

# for each simulation, run the enlarge function (see line 74) and store in a csv file
for (a in 1:sims){
	
	fname <- paste("res_",a,".csv",sep="")
	
	fp <- file(fname,"w")
	
	cat("sim",a,"\n",sep=" ")
	
	for (b in 1:length(samp)) {
	
	results <-enlarge(space = x, members=as.vector(samp[[b]]), sdnmem = 6, a = 0.75, d = 0.5, sdp = 3, R = 150)	
	cat(results,"\n",file=fp)
#	cat("sample",b,"\n",sep=" ")
	rm(results)
	}
	
close(fp)	
	
}

# set working directory to subfolder, where the results for the simulations will be stored for the following case: admit pivot = 0.67; decision pivot = 0.5
setwd("~/Dropbox/IO imperfect monitoring/GLS_II_REP/a67d50/")

# for each simulation, run the enlarge function (see line 98) and store in a csv file
for (a in 1:sims){
	
	fname <- paste("res_",a,".csv",sep="")
	
	fp <- file(fname,"w")
	
	cat("sim",a,"\n",sep=" ")
	
	for (b in 1:length(samp)) {
	
	results <-enlarge(space = x, members=as.vector(samp[[b]]), sdnmem = 6, a = 0.67, d = 0.5, sdp = 3, R = 150)	
	cat(results,"\n",file=fp)
#	cat("sample",b,"\n",sep=" ")
	rm(results)
	}
	
close(fp)	
	
}

